package licence

import (
	"crypto/md5"
	"encoding/hex"
	"math"
	"strings"
)

const (
	/*
		34进制
			0 == 0
			1 == 1
			...
			Z == 33
			10 == 34
			ZZZZ == 1336335
	*/
	n34 = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
)

func format(in string) (out string) {
	out = in
	out = strings.ReplaceAll(out, "-", "")
	out = strings.ReplaceAll(out, " ", "")
	out = strings.TrimSpace(out)
	out = strings.ToUpper(out)
	return
}

// 字符串MD5加密
func signature(str string, len int) string {
	h := md5.New()
	h.Write([]byte(str))
	result := strings.ToLower(hex.EncodeToString(h.Sum(nil)))
	// 过滤 o 和 i
	result = strings.ReplaceAll(result, "i", "")
	result = strings.ReplaceAll(result, "o", "")
	return strings.ToUpper(result[:len])
}

/*
34进制转换

	input:
		string: 34进制字符串
	output:
		int: 10进制数值 (ZZZZ => 1336335)
*/
func int34(s string) (n int) {
	for i, v := range strings.Split(s, "") {
		n += strings.Index(n34, v) * int(math.Pow(34, float64(len(s)-i-1)))
	}
	return
}

/*
34进制转换

	input:
		int: 10进制数值
		int: 长度(前置补0)
	output:
		string: 34进制字符串 (1336335 => ZZZZ)
*/
func str34(n int, i int) string {
	s := ""
	nn34 := strings.Split(n34, "")
	for {
		s = nn34[n%34] + s
		n = n / 34
		if n == 0 {
			break
		}
	}
	for len(s) < i {
		s = "0" + s
	}
	return s
}
